package com.loadmore.common.base

import android.content.Intent
import android.os.Bundle
import com.loadmore.common.model.MessageEvent
import com.loadmore.common.permission.PermissionCallback
import me.imid.swipebacklayout.lib.app.SwipeBackActivity
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import pub.devrel.easypermissions.AppSettingsDialog
import pub.devrel.easypermissions.EasyPermissions


/**
 *@author sunleilei
 *@date 2018/7/12
 *@time 8:01
 *
 */
open class BaseFragmentActivity : SwipeBackActivity(),EasyPermissions.PermissionCallbacks{

    private val permission = 1234
    private var requestPermission = false
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        EventBus.getDefault().register(this)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    open fun onMessageEvent(event: MessageEvent) {/* Do something */
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == AppSettingsDialog.DEFAULT_SETTINGS_REQ_CODE) {
            if(permissionCallback != null) {
                permissionCallback?.onSettingBack()
            }
        }
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)

        // Forward results to EasyPermissions
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this)
    }

    var permissionCallback: PermissionCallback? = null
    public fun requestPermission(perms: Array<String>,permissionCallback: PermissionCallback) {
        this.permissionCallback = permissionCallback
        requestPermission = true
        if (EasyPermissions.hasPermissions(this, *perms)) {
            // Already have permission, do the thing
            permissionCallback.onPermissionsGranted()

        } else {
            // Do not have permissions, request them now
            EasyPermissions.requestPermissions(this,"请授予权限",111,*perms)
        }
    }


    override fun onPermissionsDenied(requestCode: Int, perms: MutableList<String>) {
        requestPermission = false
        if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
            AppSettingsDialog.Builder(this).build().show()
            permissionCallback?.onPermissionsDenied()
        }
    }

    override fun onPermissionsGranted(requestCode: Int, perms: MutableList<String>) {
        permissionCallback?.onPermissionsGranted()
        requestPermission = false
    }


    override fun onStart() {
        super.onStart()
    }


    override fun onResume() {
        super.onResume()
    }

    override fun onPause() {
        super.onPause()
    }

    override fun onStop() {
        super.onStop()
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }
}